package com.dyzs.activity.money.bz;

import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.dyzs.activity.CommonListActivity;
import com.dyzs.activity.R;
import com.dyzs.client.PlanClient;
import com.dyzs.util.Constants;
import com.dyzs.util.Pager;

public class BZPlanListActivity extends CommonListActivity{
	
	private Map planMap;
	private static final int[] menu_image_array = { R.drawable.up,R.drawable.down};
	private static final String[] menu_text_array = { "上一页", "下一页"};
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pager_common_list);
		this.setTitle("报账列表");		
		emptyTV=(TextView)findViewById(android.R.id.empty);
		setGV();
		update();
	}		
	private void update() {
		showProgressDialog(getString(R.string.search_progress_title),getString(R.string.search_progress_content), true, true);
		new Thread(new Runnable() {
			public void run() {
				try {
					PlanClient planClient = new PlanClient();
					String x = planClient.updateListForBZ(Constants.USER_ID,pager.getCurrentPage()+"");
					planMap= planClient.pullListXmlForBZ(x);	
					BZPlanListActivity.this.runOnUiThread(new Runnable() {
						public void run() {
							proDialog.dismiss();
							showList();
						}
					});
				} catch (Exception e) {
					onException(e);
				}
			}
		}).start();
	}

	public void onException(final Exception e) {
		BZPlanListActivity.this.runOnUiThread(new Runnable() {
			public void run() {
				if (proDialog != null) {
					proDialog.dismiss();
				}
				emptyTV.setText(e.getMessage());						
			}
		});
	}
	
	
	private void showList() {	
		this.setTitle(planMap.get("totalNum")+"条记录");	
		if(planMap.get("totalNum").equals("0")){
			emptyTV.setText("暂无记录");
		}
		SimpleAdapter simperAdapter = new SimpleAdapter(this,
					(List<? extends Map<String, ?>>) planMap.get("list"),
					R.layout.plan_journey_item, new String[] { "line_name","plan_date"}, new int[] {R.id.plan_journey_day,R.id.plan_journey_content});
		this.setListAdapter(simperAdapter);	
		pager=new Pager(Integer.valueOf(planMap.get("totalNum").toString()),pager.getCurrentPage());	
	}
	
	
	
	
	protected void onListItemClick(ListView l, View v, int position, long id) {	;
		Intent intent = new Intent();
		intent.setClass(BZPlanListActivity.this, BZMainActivity.class);
		intent.putExtra("planId", ((Map) ((List) planMap.get("list")).get(position)).get("plan_id").toString());
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}

	

	private void setGV() {
		gv = (GridView) this.findViewById(R.id.pager_common_list_gv);
		gv.setNumColumns(2);
		gv.setAdapter(getMenuAdapter(menu_text_array, menu_image_array));
		gv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {			
				switch (arg2) {
				case 0:
					if (1 == pager.getCurrentPage()) {// 上一页置为无效
						Toast.makeText(BZPlanListActivity.this, "当前页为第一页",Toast.LENGTH_SHORT).show();
					}else{
						pager.doProvepage();
						update();
					}							
					break;
				case 1:
					if (pager.getTotalPage() == pager.getCurrentPage() || pager.getTotalPage() == 0) {
						Toast.makeText(BZPlanListActivity.this, "当前页为最后一页",Toast.LENGTH_SHORT).show();
					}else{
						pager.doNextpage();
						update();
					}
					break;
				}
			}
		});
	}
}
